package com.example.mvvmdemo.viewmodel;

import androidx.lifecycle.MutableLiveData;
import com.example.common.base.BaseViewModel;
import com.example.mvvmdemo.model.User;
import com.example.mvvmdemo.repository.UserRepository;
import com.example.common.utils.LogUtil;
import io.reactivex.rxjava3.core.Observer;
import io.reactivex.rxjava3.disposables.Disposable;
import java.util.List;

public class UserViewModel extends BaseViewModel {
    
    private UserRepository userRepository;
    
    public MutableLiveData<List<User>> userListData = new MutableLiveData<>();
    public MutableLiveData<User> userData = new MutableLiveData<>();
    
    private int currentPage = 1;
    private static final int PAGE_SIZE = 20;
    
    public UserViewModel() {
        super();
        userRepository = new UserRepository();
    }
    
    public void loadUsers(boolean isRefresh) {
        if (isRefresh) {
            currentPage = 1;
        }
        
        showLoading();
        LogUtil.d("UserViewModel", "loadUsers - page: " + currentPage);
        
        userRepository.getUsers(currentPage, PAGE_SIZE)
                .subscribe(new Observer<List<User>>() {
                    @Override
                    public void onSubscribe(Disposable d) {
                        addDisposable(d);
                    }
                    
                    @Override
                    public void onNext(List<User> users) {
                        LogUtil.d("UserViewModel", "loadUsers success, size: " + users.size());
                        if (users.isEmpty()) {
                            showEmpty();
                        } else {
                            showSuccess();
                            userListData.setValue(users);
                            currentPage++;
                        }
                    }
                    
                    @Override
                    public void onError(Throwable e) {
                        LogUtil.e("UserViewModel", "loadUsers error: " + e.getMessage());
                        showError(e.getMessage());
                    }
                    
                    @Override
                    public void onComplete() {
                        LogUtil.d("UserViewModel", "loadUsers completed");
                    }
                });
    }
    
    public void loadUserById(int userId) {
        showLoading();
        LogUtil.d("UserViewModel", "loadUserById: " + userId);
        
        userRepository.getUserById(userId)
                .subscribe(new Observer<User>() {
                    @Override
                    public void onSubscribe(Disposable d) {
                        addDisposable(d);
                    }
                    
                    @Override
                    public void onNext(User user) {
                        LogUtil.d("UserViewModel", "loadUserById success: " + user.getName());
                        showSuccess();
                        userData.setValue(user);
                    }
                    
                    @Override
                    public void onError(Throwable e) {
                        LogUtil.e("UserViewModel", "loadUserById error: " + e.getMessage());
                        showError(e.getMessage());
                    }
                    
                    @Override
                    public void onComplete() {
                        LogUtil.d("UserViewModel", "loadUserById completed");
                    }
                });
    }
    
    @Override
    public void retry() {
        super.retry();
        loadUsers(true);
    }
}